<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
class tagModel extends spModel
{
    var $pk = "tag_id"; // 每个唯一的标志，可以称为主键
    var $table = "tag"; // 数据表的名称


    function Detail($tag_id)
    {
        $condition = array('tag_id' => $tag_id);
        $arr = $this->spCache(3600 * 24)->find($condition);
        return $arr;
    }

    /**
     * 根据标签名查询是否存在
     */
    function Findtag($tag)
    {
        $condition = array('tag_name' => $tag);
        $arr = $this->find($condition);
        return $arr;
    }

    /**
     * 保存不存在的新标签
     */
    function Tagsave($tags, $tag_type = 1)
    {
        if ($tags == '')
            return false;
        $tmparray = explode(',', $tags);
        if (count($tmparray) > 1) {
            foreach ($tmparray as $k => $v) {
                $request = $this->Findtag($v);
                if (!$request)
                    $this->create(array('tag_name' => $v, 'tag_type' => $tag_type));
            }
            return true;
        } else {
            $request = $this->Findtag($tags);
            if (!$request)
            $this->create(array('tag_name' => $tags, 'tag_type' => $tag_type));
            return false;
        }
    }
}